﻿#include "QScrollLabel.h"
#include <QPainter>
#include <QDebug>
#include <QtConcurrent/QtConcurrent>

QCOMMWIDGETS_BEGIN_NAMESPACE

QScrollLabel::QScrollLabel(QFont ft,QColor color,T_textDirection direction,QWidget *parent):
QLabel(parent),m_direction(direction),m_font(ft),m_color(color)
{
    stepTime = 30;
    stepWidth = 2;
    curIndex = 0;
    m_scrollCount = 1;
    m_rowSpace = 0;
    m_drawStartY = 0;
    m_timeIdRate = 0; // 绘制速度
    m_timeIdSpace = 0; // 绘制间隔时间
    m_timeIdcontinuity = 0; // 连续启动定时器
    m_drawTimerRat = 30; 	// 绘制速度延时（30毫秒）
    m_drawTimerSpace = 30 * 1000; 	// 绘制间隔时间（30秒）
    m_isCalculationSuccess = false;
    connect(this, SIGNAL(showScrollTextMsg(QString, int, int,int)), this, SLOT(slotshowScrollText(QString, int, int,int)), Qt::QueuedConnection);
    connect(this, SIGNAL(suspendMsg()), this, SLOT(slotSuspend()), Qt::QueuedConnection);
    connect(this, SIGNAL(proceedMsg()), this, SLOT(slotProceed()), Qt::QueuedConnection);
//    scrollTimer = new QTimer();
//    connect(scrollTimer, SIGNAL(timeout()), this, SLOT(updateIndex()));
}

void QScrollLabel::showScrollText(QString text, int timerDelay, int rate,int count)
{
	emit showScrollTextMsg(text, timerDelay, rate, count);
}
// 设置显示文本
void QScrollLabel::slotshowScrollText(QString text, int timerDelay, int rate,int count)
{
	if(m_timeIdcontinuity != 0){
		this->killTimer(m_timeIdcontinuity);
		m_timeIdcontinuity = 0;
	}
	if(m_timeIdRate != 0){
		this->killTimer(m_timeIdRate);
		m_timeIdRate = 0;
	}
	if(m_drawTimerSpace != 0){
		this->killTimer(m_timeIdSpace);
		m_timeIdSpace = 0;
	}

	if(showText != text){
	    curIndex = 0;
	    m_isCalculationSuccess = false;
	}

    showText = text;
    m_scrollCount = count;

	m_drawTimerSpace = timerDelay;
	m_drawTimerRat = rate;

	m_timeIdcontinuity = startTimer(1000);
}

// 暂停
void QScrollLabel::suspend()
{
	emit suspendMsg();
}

// 继续
void QScrollLabel::proceed()
{
	emit proceedMsg();
}

//线程执行函数
void QScrollLabel::MyConcurrent()
{
	if(m_isCalculationSuccess){
		return;
	}
    calculationRowText();
	m_isCalculationSuccess = true;

}
// 暂停
void QScrollLabel::slotSuspend()
{
	if(m_timeIdRate != 0){
		this->killTimer(m_timeIdRate);
		m_timeIdRate = 0;
	}
	if(m_drawTimerSpace != 0){
		this->killTimer(m_timeIdSpace);
		m_timeIdSpace = 0;
	}
	if(m_timeIdcontinuity != 0){
		this->killTimer(m_timeIdcontinuity);
		m_timeIdcontinuity = 0;
	}
}
// 继续
void QScrollLabel::slotProceed()
{

	if(m_timeIdcontinuity != 0){
		this->killTimer(m_timeIdcontinuity);
		m_timeIdcontinuity = 0;
	}
	m_timeIdcontinuity = startTimer(1000);


}

void QScrollLabel::setRowSpace(int space)
{
    m_rowSpace = space;
}

// 设置绘制起始Y
void QScrollLabel::setDrawStartY(int y)
{
	m_drawStartY = y;
}

void QScrollLabel::calculationRowText()
{

    QFontMetrics fm(m_font);
    m_textRect = fm.boundingRect(showText);
    if(m_textRect.width() > width() &&
        m_direction == TEXT_DIRECTION_UP){

        QString textRow;
        QString textAll = showText;
        int i = textAll.count();
        m_showText.clear();
        while (width() > m_textRect.width() / showText.count()) {
            textRow = textAll.mid(0,i);
            QRect rec = fm.boundingRect(textRow);
            if(rec.width() < width()){
                m_showText.append(textRow);
                if(textAll.count() < i + 1){
                    break;
                }
                textAll = textAll.mid(i);
                i = textAll.count() + 1;
            }
            i--;
        }
    }
}

void QScrollLabel::updateIndex()
{
	if(!m_isCalculationSuccess){
		return;
	}
    curIndex++;
    if(m_direction == TEXT_DIRECTION_LEFT){

        if(stepWidth * curIndex >= m_textRect.width() + 10){
            curIndex = 0;
            if(m_scrollCount > 0)
                m_scrollCount--;
        }
    }else if(m_direction == TEXT_DIRECTION_UP){

        if(stepWidth * curIndex >= (m_textRect.height() + m_rowSpace) * m_showText.count()+m_rowSpace + 10){
            curIndex = 0;
            if(m_scrollCount > 0)
                m_scrollCount--;
        }
    }
    if(m_scrollCount <= 0){
    	if(m_timeIdRate != 0){
//    		qDebug("killTimer -> m_timeIdRate");
            this->killTimer(m_timeIdRate);
            m_timeIdRate = 0;
    	}
//        if(scrollTimer->isActive())
//            scrollTimer->stop();
    }
    update();
}

void QScrollLabel::paintEvent(QPaintEvent *event)
{
	if(!m_isCalculationSuccess){
		return;
	}
    QWidget::paintEvent(event);
    QPainter painter(this);
    painter.setFont(m_font);
    painter.setPen(m_color);
    switch (m_direction) {
    case TEXT_DIRECTION_LEFT:
    {
        // 向左移动
        if(m_textRect.width() <= width()){
            painter.drawText(0,m_textRect.height(),showText);
//            if(scrollTimer->isActive())
//                scrollTimer->stop();
        	if(m_timeIdRate != 0){

                this->killTimer(m_timeIdRate);
                m_timeIdRate = 0;
        	}
            return;
        }
        // 绘制前面的
        painter.drawText(0 - stepWidth * curIndex, m_textRect.height(), showText);
        // 绘制后面的
        painter.drawText(m_textRect.width() + 10 -(stepWidth * curIndex), m_textRect.height(), showText);
        break;
    }
    case TEXT_DIRECTION_UP:
    {
        // 向上移动
        if(m_textRect.width() < width()){
            painter.drawText(0,m_textRect.height(),showText);
//            if(scrollTimer->isActive())
//                scrollTimer->stop();
        	if(m_timeIdRate != 0){

                this->killTimer(m_timeIdRate);
                m_timeIdRate = 0;
        	}
            return;
        }
        if((m_textRect.height() + m_rowSpace) * m_showText.count()+m_rowSpace <= height()){
            for (int i = 0; i < m_showText.count(); i++) {
                painter.drawText(0, m_drawStartY + (m_textRect.height()+m_rowSpace)*(i+1), m_showText[i]);
            }
//            if(scrollTimer->isActive())
//                scrollTimer->stop();
        	if(m_timeIdRate != 0){

                this->killTimer(m_timeIdRate);
                m_timeIdRate = 0;
        	}
            return;
        }

        // 绘制前面的
        for (int i = 0; i < m_showText.count(); i++) {
            painter.drawText(0, m_drawStartY + (m_textRect.height()+m_rowSpace)*(i+1) - (stepWidth * curIndex), m_showText[i]);
        }

        // 绘制后面的
        for (int i = 0; i < m_showText.count(); i++) {
            painter.drawText(0, m_drawStartY + (m_textRect.height()+m_rowSpace)*(i+1) + ((m_textRect.height() + m_rowSpace) * m_showText.count()+m_rowSpace + 10 -(stepWidth * curIndex)), m_showText[i]);
        }
        break;
    }
    default:
        break;
    }
}

void QScrollLabel::resizeEvent(QResizeEvent *event)
{
	// 创建线程执行初始化
	QtConcurrent::run(this,&QScrollLabel::MyConcurrent);

}

void QScrollLabel::timerEvent(QTimerEvent *event)
{

    if(event->timerId() == m_timeIdRate)
    {
    	// 绘制速度
    	updateIndex();
    }
    else if(event->timerId() == m_timeIdcontinuity)
    {
    	this->killTimer(m_timeIdcontinuity);
    	m_timeIdcontinuity = 0;
    	// 创建线程执行初始化
    	QtConcurrent::run(this,&QScrollLabel::MyConcurrent);
    	if(m_timeIdRate == 0){

        	m_timeIdRate = startTimer(m_drawTimerRat);
    	}
    	if(m_timeIdSpace == 0){
        	m_timeIdSpace = startTimer(m_drawTimerSpace);
    	}
    }
    else if(event->timerId() == m_timeIdSpace)
    {
    	// 绘制间隔时间
    	m_scrollCount = 1;
    	if(m_timeIdRate == 0){
        	m_timeIdRate = startTimer(m_drawTimerRat);
    	}
//    	qDebug("m_timeIdSpace start");
    }
}
QCOMMWIDGETS_END_NAMESPACE
